﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.Entity;
using System.Web.Mvc;
using KPIS.GERP.Models;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.Models.Class;

namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface IRequestParcelService
    {
        #region GetString[]

        [OperationContract]
        string[] GetJobsite(int intUserId);

        [OperationContract]
        string GetNameRequestBy(int intReqById);

        [OperationContract]
        string GetUnitName(string strType, int intResItemId);

        [OperationContract]
        string GetStatusName(string strRecordStatus);

        #endregion

        #region RequestParcel

        [OperationContract]
        IEnumerable<RequestParcel> SelectRequestParcel(RequestParcel criteriaModel = null,
            List<string> includeSubObjectString = null);

        [OperationContract]
        int Insert(RequestParcel model, int createByUserId);

        [OperationContract]
        void Update(RequestParcel model, int updateByUserId);

        [OperationContract]
        void Delete(int id, int deleteByUserId);

        #endregion

        #region RequestParcelDtl

        [OperationContract]
        IEnumerable<RequestParcelDtl> SelectRequestParcelDtl(RequestParcelDtl criteriaModel = null,
            List<string> includeSubObjectString = null);

        [OperationContract]
        int InsertDtl(RequestParcelDtl model, int createByUserId);

        [OperationContract]
        void UpdateDtl(RequestParcelDtl model, int updateByUserId);

        [OperationContract]
        void DeleteDtl(int id, int deleteByUserId);

        #endregion

        #region SelectListItem

        [OperationContract]
        List<SelectListItem> GetProject(int intUserId);

        [OperationContract]
        List<SelectListItem> GetWarehouse(int intUserId);

        [OperationContract]
        List<SelectListItem> GetRequestBy(int intUserId);

        [OperationContract]
        List<SelectListItem> MasLookUpSelectList(IcmMasLookUpType.LookUpType lookUpType);

        [OperationContract]
        List<SelectListItem> ParcelSelectList(string ParcelType, int intProjectId, int intWhId);

        #endregion

        #region GetQuantity

        [OperationContract]
        int GetQuantity(string strType, int intResItemId, int? intProjId, int intWhId);

        #endregion

        #region Send Request For Approved

        [OperationContract]
        void UpdateRequestForApproved(int intReqId, int updateByUserId);

        #endregion

        #region  Report

        [OperationContract]
        List<Dictionary<string, string>> ReqItem(int ReqParcelId);

        #endregion
    }

    public class RequestParcelService : IRequestParcelService
    {
        #region GetString[]

        public string[] GetJobsite(int intUserId)
        {
            string strJobsiteSeq;
            string strJobsiteName;
            var context = new GERPContext(false);
            try
            {
                var JobsiteName = (from m in context.Employees
                    join a in context.JobSites on m.WorkingJobSiteId equals a.JobSiteId
                    where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                          && m.EmpId == intUserId
                    select a.Name).ToList();
                strJobsiteName = JobsiteName[0].ToString();

                var JobsiteSeq = (from m in context.Employees
                    join a in context.JobSites on m.WorkingJobSiteId equals a.JobSiteId
                    where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                          && m.EmpId == intUserId
                    select a.JobSiteId).ToList();
                int intJobsiteSeq = Convert.ToInt32(JobsiteSeq[0]);
                strJobsiteSeq = intJobsiteSeq.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return new string[] {strJobsiteName, strJobsiteSeq};
        }

        public string GetNameRequestBy(int intReqById)
        {
            string strFirstName;
            string strLastName;
            string strFullName;
            var context = new GERPContext(false);
            try
            {
                var EmpName = (from m in context.Employees
                    where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                          && m.EmpId == intReqById
                    select m).ToList();
                strFirstName = EmpName[0].FirstName.ToString();
                strLastName = EmpName[0].LastName.ToString();
                strFullName = strFirstName + "  " + strLastName;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return strFullName;
        }

        public string GetUnitName(string strType, int intResItemId)
        {
            string strUnitName;
            var context = new GERPContext(false); 
            if (strType == "OS")
            { 
                strUnitName = (from a in context.OfficeSuppliesLots
                               join b in context.MasUnits on a.UnitId equals b.UnitId
                               where a.RecordStatus != RecordStatus.DeletedStatus
                                     && a.ResItemId == intResItemId
                               select b.Name).FirstOrDefault();
            }
            else
            {
                strUnitName = (from a in context.Assets
                               join b in context.MasUnits on a.UnitId equals b.UnitId
                               where a.RecordStatus != RecordStatus.DeletedStatus
                                       && a.ResItemId == intResItemId
                               select b.Name).FirstOrDefault();
            } 
            return strUnitName;
        }

        public string GetStatusName(string strRecordStatus)
        {
            string strResault = "";
            var context = new GERPContext(false);
            try
            {
                switch (strRecordStatus)
                {
                    case "A":
                        strResault = "ได้รับการอนุมัติ";
                        break;
                    case "C":
                        strResault = "ยกเลิกแล้ว";
                        break;
                    case "D":
                        strResault = "ลบแล้ว";
                        break;
                    case "P":
                        strResault = "รอการพิจารณา";
                        break;
                    case "R":
                        strResault = "ส่งกลับ";
                        break;
                    case "U":
                        strResault = "ใช้งานอยู่";
                        break;
                    case "W":
                        strResault = "ดำเนินการอยู่";
                        break;
                    default:
                        strResault = "ไม่มีสถานะ (" + strRecordStatus + ")";
                        break;
                }


            }
            catch (Exception ex)
            {
                throw ex;
            }

            return strResault;
        }


        #endregion

        #region RequestParcel

        public IEnumerable<RequestParcel> SelectRequestParcel(RequestParcel criteriaModel = null,
            List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<RequestParcel> query = context.RequestParcels;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query,
                            (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(
                            KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insert(RequestParcel model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    model.RecordStatus = RecordStatus.WaitStatus;
                    model.CreatedBy = createByUserId;
                    //model.Code = CodeGenerate.GetNextCode(CodeGenerate.TransectionType.RequestParcel);
                    context.RequestParcels.Add(model);
                    context.SaveChanges();

                    return model.ReqParcelId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void Update(RequestParcel model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.RequestParcels.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region RequestParcelDtl

        public IEnumerable<RequestParcelDtl> SelectRequestParcelDtl(RequestParcelDtl criteriaModel = null,
            List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<RequestParcelDtl> query = context.RequestParcelDtls;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query,
                            (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(
                            KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertDtl(RequestParcelDtl model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;

                    context.RequestParcelDtls.Add(model);
                    context.SaveChanges();

                    return model.ReqDtlId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void UpdateDtl(RequestParcelDtl model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteDtl(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.RequestParcelDtls.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        #endregion

        #region SelectListItem

        public List<SelectListItem> GetProject(int intEmpId)
        {
            var data = new List<SelectListItem>();
            var context = new GERPContext(false);
            try
            {
                //int intEmpId = Convert.ToInt32( (from a in context.Employees
                //                join b in context.Users on a.EmpId equals b.EmpId
                //                where b.UserId == intUserId
                //                && a.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                //                && b.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                //                select b.EmpId).FirstOrDefault());

                List<Models.BGM.Project> listProject = (from m in context.Projects
                    //join job in context.JobSites on m.JobSiteId equals job.JobSiteId
                   // join emp in context.Employees on m.JobSiteId equals emp.WorkingJobSiteId
                    where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                          // && emp.EmpId == intEmpId
                    select m).ToList();

                data.Add(new SelectListItem {Text = "--ไม่ระบุโครงการ--", Value = ""});

                if (listProject.Count > 0)
                {
                    foreach (var Project in listProject)
                    {
                        var item = new SelectListItem
                        {
                            Text = Project.Code + ":" + Project.Name,
                            Value = Project.ProjId.ToString()
                        };
                        data.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }

        public List<SelectListItem> GetWarehouse(int intEmpId)
        {
            var data = new List<SelectListItem>();
            var context = new GERPContext();
            SelectListItem item = null;
            try
            {
                //int intEmpId = Convert.ToInt32((from a in context.Employees
                //                                join b in context.Users on a.EmpId equals b.EmpId
                //                                where b.UserId == intUserId
                //                                && a.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                //                                && b.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                //                                select b.EmpId).FirstOrDefault());

                List<Models.ICM.WarehousePermit> listWarehousePermit = (from m in context.WarehousePermits
                    //join job in context.JobSites on m.JobSiteId equals job.JobSiteId
                    join emp in context.Employees on m.JobSiteId equals emp.WorkingJobSiteId
                    where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                          && emp.EmpId == intEmpId
                    select m).ToList();

                foreach (var WarehousePermit in listWarehousePermit)
                {
                    int id = WarehousePermit.WhId;

                    var txt = (from a in context.Warehouses
                        where a.WhId == id && a.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                        select a.Name).ToList();

                    if (txt.Count > 0)
                    {
                        item = new SelectListItem
                        {
                            Text = txt[0].ToString(),
                            Value = WarehousePermit.WarehousePermitId.ToString()
                        };
                        data.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }

        public List<SelectListItem> GetRequestBy(int intEmpId)
        {
            var data = new List<SelectListItem>();
            var context = new GERPContext(false);
            try
            {
                int intJobsiteSeq = context.Employees.Find(intEmpId).WorkingJobSiteId ?? 0;


                List<Models.HRM.Employee> listEmployee = (from emp in context.Employees
                    join job in context.JobSites on emp.WorkingJobSiteId equals job.JobSiteId
                    join es in context.PsEmployeeStatus on emp.EmpStatusId equals es.EmpStatusId
                    where emp.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                          && es.Active == "1"
                          && job.JobSiteId == intJobsiteSeq
                    select emp).ToList();

                foreach (var Employee in listEmployee)
                {
                    var item = new SelectListItem
                    {
                        Text = Employee.FirstName + "  " + Employee.LastName,
                        Value = Employee.EmpId.ToString()
                    };
                    data.Add(item);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }

        public List<SelectListItem> MasLookUpSelectList(IcmMasLookUpType.LookUpType lookUpType)
        {
            var data = new List<SelectListItem>();
            var context = new GERPContext(false);
            try
            {
                List<Models.ICM.IcmMasLookUp> listMasLookUp = (from m in context.IcmMasLookUps
                    where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                          && m.LookUpTypeId == (int) lookUpType
                    select m).ToList();

                foreach (var MasLookUp in listMasLookUp)
                {
                    var item = new SelectListItem {Text = MasLookUp.LookUpName, Value = MasLookUp.Description};
                    if (MasLookUp.LookUpId == (int) lookUpType) item.Selected = true;
                    data.Add(item);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }

        public List<SelectListItem> ParcelSelectList(string ParcelType, int intProjectId, int intWhId)
        {
            var data = new List<SelectListItem>();
            var listMasResourceItem = new List<Models.ICM.MasResourceItem>();

            var context = new GERPContext(false);

            var queuryFindProjExpId = (from f in context.ProjectExpenses
                join g in context.Projects on f.ProjId equals g.ProjId
                where g.ProjId == intProjectId
                select (int?)f.ProjExpId);

            int?[] intProjExpIds = queuryFindProjExpId.ToArray();

            try
            {
                switch (ParcelType)
                {
                    case "OS":

                        var qu = (from a in context.MasResourceItems
                            join b in context.MasResourceSubGroups on a.ResSubGrpId equals b.ResSubGrpId
                            join c in context.MasResourceGroups on b.ResGrpId equals c.ResGrpId
                            join d in context.MasResourceTypes on c.ResTypeId equals d.ResTypeId
                            join e in context.OfficeSuppliesLots on a.ResItemId equals e.ResItemId
                            //join f in context.ProjectExpenses on e.ProjExpId equals f.ProjExpId
                            //join g in context.Projects on f.ProjId equals g.ProjId
                            where (a.RecordStatus ?? "") != RecordStatus.DeletedStatus &&
                                  e.WhId == intWhId &&
                                  (!e.ProjExpId.HasValue || intProjExpIds.Contains(e.ProjExpId))
                            select a)
                            .GroupBy(o => o.ResItemId)
                            .Select(o => o.FirstOrDefault());

                        listMasResourceItem = qu.ToList().Where(o => o != null).ToList();
                        foreach (var MasResourceItem in listMasResourceItem)
                        {
                            var item = new SelectListItem
                            {
                                Text = MasResourceItem.Code + " : " + MasResourceItem.Name,
                                Value = MasResourceItem.ResItemId.ToString()
                            };
                            data.Add(item);
                        }
                        break;

                    case "A":
                        listMasResourceItem = (from a in context.MasResourceItems
                            join b in context.MasResourceSubGroups on a.ResSubGrpId equals b.ResSubGrpId
                            join c in context.MasResourceGroups on b.ResGrpId equals c.ResGrpId
                            join d in context.MasResourceTypes on c.ResTypeId equals d.ResTypeId
                            join e in context.Assets on a.ResItemId equals e.ResItemId
                            //join f in context.ProjectExpenses on e.ProjExpId equals f.ProjExpId
                            //join g in context.Projects on f.ProjId equals g.ProjId
                            where (intProjExpIds.Contains(e.ProjExpId) || e.ProjExpId == null)
                                  && a.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                                  && e.WhId == intWhId
                            select a)
                            .GroupBy(o => o.ResItemId)
                            .Select(o => o.FirstOrDefault())
                            .ToList().Where(o => o != null).ToList();

                        foreach (var MasResourceItem in listMasResourceItem)
                        {
                            var item = new SelectListItem
                            {
                                Text = MasResourceItem.Code + " : " + MasResourceItem.Name,
                                Value = MasResourceItem.ResItemId.ToString()
                            };
                            data.Add(item);
                        }
                        break;
                }



            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }

        #endregion

        #region GetQuantity

        public int GetQuantity(string strType, int intResItemId, int? intProjId, int intWhId)
        {
            int Quantity = 0;

            var context = new GERPContext(false);

            // 21/5/2558 ไม่ต้องคำนึงโครงการ ก็สามารถเบิกได้
            //int intProjExpId = (from f in context.ProjectExpenses
            //    join g in context.Projects on f.ProjId equals g.ProjId
            //    where g.ProjId == intProjId
            //    select g.ProjId).FirstOrDefault();

            try
            {
                if (strType == "OS")
                {
                    var ListQuantity = (
                        from a in context.OfficeSuppliesLots.Include("ReceiveDtl")
                        where
                            // 21/5/2558 ไม่ต้องคำนึงโครงการ ก็สามารถเบิกได้ a.ProjExpId == intProjExpId || a.ProjExpId == null
                            a.ResItemId == intResItemId
                            && a.WhId == intWhId
                            && a.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                        select a).ToList();

                    var list = ListQuantity
                        // 21/5/2558 ไม่ต้องคำนึงโครงการ ก็สามารถเบิกได้ .Where(x => x.ProjExpId == intProjExpId || x.ProjExpId == null)
                        .ToList();

                    decimal Quantityd = list.Where(o => o.OfficeSupplyLotStatusId == 83).Sum(x => x.ReceiveDtl.Quantity);

                    var lis = context.RequestParcelDtls
                        .Include(o => o.RequestParcel.WarehousePermit.Warehouse)
                        .Include(o => o.MasResourceItem.MasUnit).ToList();

                    var ListQuantityDispense = lis.Where(a => a.RecordStatus != "D" &&
                                                              (a.RequestParcel == null ||
                                                               a.RequestParcel.RecordStatus !=
                                                               RecordStatus.DeletedStatus) &&
                                                              a.ResItemId == intResItemId);
                    // && a.RequestParcel.WhId == intWhId 

                    var qD = ListQuantityDispense.Sum(o => o.Quantity ?? 0);

                    Quantity = Convert.ToInt32(Quantityd - qD);




                }
                else
                {
                    var ListQuantity = (
                        from a in context.Assets
                        where
                            //21/5/2558 ไม่ต้องคำนึงโครงการ ก็สามารถเบิกได้ a.ProjExpId == intProjExpId || a.ProjExpId == null
                            a.ResItemId == intResItemId
                            && a.WhId == intWhId
                            && a.JobSiteId == null // && a.Place == null
                            && a.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                        select a).ToList();

                    var list = ListQuantity
                        // 21/5/2558 ไม่ต้องคำนึงโครงการ ก็สามารถเบิกได้ .Where(x => x.ProjExpId == intProjExpId || x.ProjExpId == null)
                        .ToList();

                    Quantity = ListQuantity.Sum(x => x.Quantity);

                    var assetIds = list.Select(o => o.AssetId).ToArray();

                    var listMm = context.MovementHistorys.Include("Asset")
                        .Where(a => assetIds.Contains(a.AssetId) && a.RecordStatus != "D")
                        .OrderByDescending(o => o.CreatedWhen);

                    foreach (var l in listMm)
                    {
                        Quantity -= 1;
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return Quantity;
        }

        public int SumQuantity(int bidderQuoId)
        {
            int Quantity = 0;

            try
            {
                using (var context = new GERPContext())
                {
                    var ListQuantity = (from a in context.PurchaseOrderInstallmentItems
                        where a.BidderQuoId == bidderQuoId
                        select a);

                    Quantity = ListQuantity.Sum(a => a.Qty) ?? 0;

                    return Quantity;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion

        #region Send Request For Approved

        public void UpdateRequestForApproved(int intReqId, int updateByUserId)
        {
            try
            {

                using (var context = new GERPContext())
                {
                    var model = (from a in context.RequestParcels
                        where a.ReqParcelId == intReqId && a.RecordStatus != "D"
                        select a).FirstOrDefault();

                    context.Entry(model).State = EntityState.Modified;
                    model.RecordStatus = RecordStatus.ProcessStatus;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Report

        public List<Dictionary<string, string>> ReqItem(int ReqParcelId)
        {
            GERPContext context = new GERPContext();

            var x = (from a in context.RequestParcelDtls
                where a.ReqId == ReqParcelId
                select new {code = a.MasResourceItem.Code, a.ItemName, a.Quantity}).ToList();

            List<Dictionary<string, string>> Dics = new List<Dictionary<string, string>>();

            int nIndex = 1;
            foreach (var item in x)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();

                dic.Add("No", nIndex.ToString());
                dic.Add("ResitemCode", item.code);
                dic.Add("ResItemName", ResolveItemName(item.ItemName, item.code));
                dic.Add("Qty", item.Quantity.HasValue ? item.Quantity.Value.ToString("#,##0") : "-");

                Dics.Add(dic);
                nIndex++;
            }

            return Dics;
        }

        private string ResolveItemName(string itemName, string itemCode)
        {
            // แก้ไข จาก | ลำดับ | 101081-00033 | 101081-00033 : หนังสือมาตรฐานโรงพยาบาลและบริการสุขภาพ (ภาษาไทย) | จำนวนเงิน
            // แก้ไข เป็น | ลำดับ | 101081-00033 | หนังสือมาตรฐานโรงพยาบาลและบริการสุขภาพ (ภาษาไทย) | จำนวนเงิน
            itemName = (itemName ?? string.Empty).TrimStart();

            if (itemName.StartsWith(itemCode + " :"))
            {
                itemName = itemName.Replace(itemCode + " :", "");
            }
            else if (itemName.StartsWith(itemCode + ":"))
            {
                itemName = itemName.Replace(itemCode + ":", "");
            } 

            return itemName.Trim(); 
        }

        #endregion
    }
}